/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.opengpstracks.logger;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.lcdui.laf.ColorSchema;
import org.netbeans.microedition.lcdui.laf.UserColorSchema;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author raindrop
 */
public class OpenGPSTracksLogger extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command exitCommand1;
    private Command okCommand;
    private Command stopCommand;
    private Command exitCommand2;
    private Command okCommand1;
    private Command okCommand2;
    private Command cancelCommand;
    private Form form;
    private StringItem stringItem;
    private SplashScreen splashScreen;
    private WaitScreen waitScreen;
    private Form form2;
    private StringItem stringItem3;
    private Form form1;
    private StringItem stringItem1;
    private Spacer spacer;
    private StringItem stringItem2;
    private StringItem stringItem5;
    private Form loginForm;
    private Spacer spacer1;
    private StringItem stringItem4;
    private TextField screenNameField;
    private Alert alert;
    private SimpleCancellableTask task;
    private Ticker ticker;
    private Image image2;
    private Image image1;
    private Font font;
    private Image image3;
    //</editor-fold>//GEN-END:|fields|0|

    //<editor-fold defaultstate="collapsed" desc="Custom Fields ">
    String m_ScreenName;
    String m_LogSessionID;
    String m_LastError;

    boolean m_CancelUpdateTask;
    SimpleCancellableTask m_UpdateTask;
    Thread m_UpdateThread;
    String m_DefaultURL = "http://log.opengpstracks.net/";
    String m_UserAgent = "OpenGPSTracksLogger/1.0";

    Criteria m_Criteria;
    LocationProvider m_LP;
    Location m_Location;
    Coordinates  m_Coordinates;
    UserColorSchema m_whiteOnBlackSchema;
    //</editor-fold>

    /**
     * The OpenGPSTracksLogger constructor.
     */
    public OpenGPSTracksLogger() {
        LocalizationSupport.initLocalizationSupport();
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == alert) {//GEN-BEGIN:|7-commandAction|1|101-preAction
            if (command == okCommand2) {//GEN-END:|7-commandAction|1|101-preAction
                // write pre-action user code here
                switchDisplayable(null, getLoginForm());//GEN-LINE:|7-commandAction|2|101-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|19-preAction
        } else if (displayable == form) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|3|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|19-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|5|36-preAction
                // write pre-action user code here
                switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|6|36-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|69-preAction
        } else if (displayable == form1) {
            if (command == stopCommand) {//GEN-END:|7-commandAction|7|69-preAction
                // write pre-action user code here
                stopUpdateTask();//GEN-LINE:|7-commandAction|8|69-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|67-preAction
        } else if (displayable == form2) {
            if (command == exitCommand2) {//GEN-END:|7-commandAction|9|67-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|10|67-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|97-preAction
        } else if (displayable == loginForm) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|11|97-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|12|97-postAction
                // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|13|95-preAction
                // write pre-action user code here
                processLogin();//GEN-LINE:|7-commandAction|14|95-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|31-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|15|31-preAction
                // write pre-action user code here
                switchDisplayable(null, getLoginForm());//GEN-LINE:|7-commandAction|16|31-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|58-preAction
        } else if (displayable == waitScreen) {
            if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|7-commandAction|17|58-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm2());//GEN-LINE:|7-commandAction|18|58-postAction
                // write post-action user code here
            } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|7-commandAction|19|57-preAction
                // write pre-action user code here
                startUpdateTask();//GEN-LINE:|7-commandAction|20|57-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|7-postCommandAction
        }//GEN-END:|7-commandAction|21|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|22|
    //</editor-fold>//GEN-END:|7-commandAction|22|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command(LocalizationSupport.getMessage("logger.exit"), LocalizationSupport.getMessage("logger.exit"), Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form(LocalizationSupport.getMessage("logger.title"), new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getExitCommand());
            form.addCommand(getOkCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            stringItem = new StringItem("", LocalizationSupport.getMessage("logger.start"), Item.PLAIN);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|22-getter|2|


    ColorSchema getColorSchema() {
        if (m_whiteOnBlackSchema == null) {
            m_whiteOnBlackSchema = new UserColorSchema();
            m_whiteOnBlackSchema.setBGColor(0xffffff);
            m_whiteOnBlackSchema.setFGColor(0x000000);
        }
        return m_whiteOnBlackSchema;
    }
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|29-getter|1|29-postInit
            splashScreen.setTitle(LocalizationSupport.getMessage("logger.title"));
            splashScreen.setCommandListener(this);
            splashScreen.setImage(getImage1());
            splashScreen.setText(LocalizationSupport.getMessage("logger.project_by"));//GEN-END:|29-getter|1|29-postInit
          // write post-init user code here

            splashScreen.setColorSchema( getColorSchema() );

        }//GEN-BEGIN:|29-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|29-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            okCommand = new Command("Start", LocalizationSupport.getMessage("command.logger_start"), Command.OK, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    String requestSessionID() {
        String  newSessionID = "";
        m_ScreenName = screenNameField.getString();
        String data = "/?new-session=" + m_ScreenName;

        try {
            newSessionID = sendPostRequest(data);
        } catch (java.io.IOException ex) {
            return "ERROR";
        }
        return newSessionID;
    }


    boolean closeSession(String sid)
    {
        String status = "";
        String data = "/?stop-session=" + sid;

        try {
            status  = sendPostRequest(data);
        } catch (java.io.IOException ex) {
            return false;
        }
        return (!status.equals("OK"));
    }

    boolean authenticateUser() {
        
        m_ScreenName = screenNameField.getString();
        String data = "/?check=" + m_ScreenName;
        try {
            String status  = sendPostRequest(data);

            if (status.equals("") && m_LogSessionID.startsWith("ERROR")) {
                return false;
            }
            return true;
        } catch (Exception e){
            return false;
        }
    }
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: processLogin ">//GEN-BEGIN:|47-if|0|47-preIf
    /**
     * Performs an action assigned to the processLogin if-point.
     */
    public void processLogin() {//GEN-END:|47-if|0|47-preIf
        // enter pre-if user code here
        if (authenticateUser()) {//GEN-LINE:|47-if|1|48-preAction
            // write pre-action user code here
            switchDisplayable(null, getForm());//GEN-LINE:|47-if|2|48-postAction
            // write post-action user code here
        } else {//GEN-LINE:|47-if|3|49-preAction
            // write pre-action user code here
            switchDisplayable(null, getAlert());//GEN-LINE:|47-if|4|49-postAction
            // write post-action user code here

        }//GEN-LINE:|47-if|5|47-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|47-if|6|
    //</editor-fold>//GEN-END:|47-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitScreen ">//GEN-BEGIN:|54-getter|0|54-preInit
    /**
     * Returns an initiliazed instance of waitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getWaitScreen() {
        if (waitScreen == null) {//GEN-END:|54-getter|0|54-preInit
            // write pre-init user code here
            waitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|54-getter|1|54-postInit
            waitScreen.setTitle(LocalizationSupport.getMessage("logger.title"));
            waitScreen.setCommandListener(this);
            waitScreen.setImage(getImage3());
            waitScreen.setText(LocalizationSupport.getMessage("logger.init"));
            waitScreen.setTask(getTask());//GEN-END:|54-getter|1|54-postInit
            // write post-init user code here
            waitScreen.setColorSchema( getColorSchema() );
        }//GEN-BEGIN:|54-getter|2|
        return waitScreen;
    }
    //</editor-fold>//GEN-END:|54-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|59-getter|0|59-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|59-getter|0|59-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|59-getter|1|59-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|59-getter|1|59-execute

                    String accuracy = sendPostRequest("/?getaccuracy");
                    int nAccuracy = Integer.parseInt(accuracy);
                    try {
                        nAccuracy = Integer.parseInt(accuracy);
                    }catch(Exception e){
                        nAccuracy = 50;
                    }


                    try {
                        // Set criteria for selecting a location provider:
                        m_Criteria = new Criteria();

                        m_Criteria.setHorizontalAccuracy(nAccuracy);

                        // Get an instance of the provider
                        m_LP = LocationProvider.getInstance(m_Criteria);

                        // Request the location, setting a one-minute timeout
                        m_Location = m_LP.getLocation(60);
                        m_Coordinates = m_Location.getQualifiedCoordinates();
                        if (m_Coordinates == null) {
                            throw new Exception("invalid coordinates");
                        }
                    } catch (Exception e){
                        throw e;
                    }
                }//GEN-BEGIN:|59-getter|2|59-postInit
            });//GEN-END:|59-getter|2|59-postInit
            // write post-init user code here
        }//GEN-BEGIN:|59-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|59-getter|3|


    SimpleCancellableTask getCoordinateUpdateTask(){

        if (m_UpdateTask == null){
            m_UpdateTask = new SimpleCancellableTask();
            m_UpdateTask.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {
                    if (m_LogSessionID != null && !m_LogSessionID.equals("")){
                        closeSession(m_LogSessionID);
                    }

                    m_LogSessionID = requestSessionID();
                    while (!m_CancelUpdateTask){
                        try {
                            m_Location = m_LP.getLocation(60);
                            m_Coordinates = m_Location.getQualifiedCoordinates();
                            if (m_Coordinates != null) {
                                try {
                                    stringItem2.setText(m_Coordinates.getLongitude() +
                                        " " + m_Coordinates.getLatitude());
                                } catch (Exception e){
                                    e.printStackTrace();
                                }
                            }

                            String data = "?lat=" + m_Coordinates.getLatitude() +
                                    "&lon="+ m_Coordinates.getLongitude() +
                                    "&alt=" + m_Coordinates.getAltitude() +
                                    "&sid=" + m_LogSessionID;
                            
                            String result = sendPostRequest(data);
                            // stringItem5.setText("HTTP: " + result);
                        } catch (Exception e){
                        }
                        Thread.sleep(2000);
                    }
                    
                    closeSession(m_LogSessionID);
                    m_LogSessionID = "";                    
                }
            });
        }
        return m_UpdateTask;
    }

    protected void setupSystemProperties()
    {
        String conf;
        String profiles;
        String platform;

        conf = System.getProperty("microedition.configuration");
        profiles = System.getProperty("microedition.profiles");
        platform = System.getProperty("microedition.platform");

        
        m_UserAgent = "OpenGPSTracksLogger/1.0 Profile/" + profiles +
                " Configuration/" + conf + (platform!=null ? " Platform/" + platform : "");
        
    }
    
    // send a POST request to Web server
    public String sendPostRequest(String requeststring) throws IOException {
            HttpConnection hc = null;
            DataInputStream dis = null;
            DataOutputStream dos = null;
            StringBuffer messagebuffer = new StringBuffer();
            
            try {
                    // Open up a http connection with the Web server
                    // for both send and receive operations
                    hc = (HttpConnection) Connector.open(m_DefaultURL + requeststring, Connector.READ_WRITE);
                    hc.setRequestProperty("User-Agent", m_UserAgent);

                    // Set the request method to POST
                    hc.setRequestMethod(HttpConnection.GET);

                    // Retrieve the response back from the servlet
                    dis = new DataInputStream(hc.openInputStream());
                    int ch;
                    // Check the Content-Length first
                    long len = hc.getLength();
                    if(len!=-1) {
                        for(int i = 0; i < len;i++) {
                            if((ch = dis.read())!= -1) {
                                    messagebuffer.append((char)ch);
                            } else {
                                    // if the content-length is not available
                                    while ((ch = dis.read()) != -1) {
                                            messagebuffer.append((char) ch);
                                    }
                            }
                        }
                    }
                    dis.close();
                    
            } catch (IOException ioe) {
                    messagebuffer = new StringBuffer("ERROR!");
            } finally {
                    // Free up i/o streams and http connection
                    try {
                            if (hc != null) hc.close();
                    } catch (IOException ignored) {}
                    try {
                            if (dis != null) dis.close();
                    } catch (IOException ignored) {}
                    try {
                            if (dos != null) dos.close();
                    } catch (IOException ignored) {}
            }
            return messagebuffer.toString();
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form1 ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of form1 component.
     * @return the initialized component instance
     */
    public Form getForm1() {
        if (form1 == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            form1 = new Form(LocalizationSupport.getMessage("logger.title"), new Item[] { getStringItem1(), getSpacer(), getStringItem2(), getStringItem5() });//GEN-BEGIN:|61-getter|1|61-postInit
            form1.addCommand(getStopCommand());
            form1.setCommandListener(this);//GEN-END:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return form1;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form2 ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of form2 component.
     * @return the initialized component instance
     */
    public Form getForm2() {
        if (form2 == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            form2 = new Form(LocalizationSupport.getMessage("logger.title"), new Item[] { getStringItem3() });//GEN-BEGIN:|63-getter|1|63-postInit
            form2.addCommand(getExitCommand2());
            form2.setCommandListener(this);//GEN-END:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return form2;
    }
    //</editor-fold>//GEN-END:|63-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|66-getter|0|66-preInit
    /**
     * Returns an initiliazed instance of exitCommand2 component.
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|66-getter|0|66-preInit
            // write pre-init user code here
            exitCommand2 = new Command(LocalizationSupport.getMessage("logger.exit"), LocalizationSupport.getMessage("logger.exit"), Command.EXIT, 0);//GEN-LINE:|66-getter|1|66-postInit
            // write post-init user code here
        }//GEN-BEGIN:|66-getter|2|
        return exitCommand2;
    }
    //</editor-fold>//GEN-END:|66-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stopCommand ">//GEN-BEGIN:|68-getter|0|68-preInit
    /**
     * Returns an initiliazed instance of stopCommand component.
     * @return the initialized component instance
     */
    public Command getStopCommand() {
        if (stopCommand == null) {//GEN-END:|68-getter|0|68-preInit
            // write pre-init user code here
            stopCommand = new Command("Stop", Command.STOP, 0);//GEN-LINE:|68-getter|1|68-postInit
            // write post-init user code here
        }//GEN-BEGIN:|68-getter|2|
        return stopCommand;
    }
    //</editor-fold>//GEN-END:|68-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|72-getter|0|72-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|72-getter|0|72-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Status:", LocalizationSupport.getMessage("logger.active"), Item.PLAIN);//GEN-LINE:|72-getter|1|72-postInit
            // write post-init user code here
        }//GEN-BEGIN:|72-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|72-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|74-getter|0|74-preInit
    /**
     * Returns an initiliazed instance of spacer component.
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|74-getter|0|74-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 10);//GEN-LINE:|74-getter|1|74-postInit
            // write post-init user code here
        }//GEN-BEGIN:|74-getter|2|
        return spacer;
    }
    //</editor-fold>//GEN-END:|74-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem2 ">//GEN-BEGIN:|75-getter|0|75-preInit
    /**
     * Returns an initiliazed instance of stringItem2 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem2() {
        if (stringItem2 == null) {//GEN-END:|75-getter|0|75-preInit
            // write pre-init user code here
            stringItem2 = new StringItem(LocalizationSupport.getMessage("logger.location"), "", Item.PLAIN);//GEN-LINE:|75-getter|1|75-postInit
            // write post-init user code here
        }//GEN-BEGIN:|75-getter|2|
        return stringItem2;
    }
    //</editor-fold>//GEN-END:|75-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem3 ">//GEN-BEGIN:|78-getter|0|78-preInit
    /**
     * Returns an initiliazed instance of stringItem3 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem3() {
        if (stringItem3 == null) {//GEN-END:|78-getter|0|78-preInit
            // write pre-init user code here
            stringItem3 = new StringItem("", LocalizationSupport.getMessage("error.gps"));//GEN-LINE:|78-getter|1|78-postInit
            // write post-init user code here
        }//GEN-BEGIN:|78-getter|2|
        return stringItem3;
    }
    //</editor-fold>//GEN-END:|78-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startUpdateTask ">//GEN-BEGIN:|81-entry|0|82-preAction
    /**
     * Performs an action assigned to the startUpdateTask entry-point.
     */
    public void startUpdateTask() {//GEN-END:|81-entry|0|82-preAction
        if (m_UpdateThread==null)
            m_UpdateThread = new Thread(getCoordinateUpdateTask());

        m_CancelUpdateTask = false;
        m_UpdateThread.start();

        switchDisplayable(null, getForm1());//GEN-LINE:|81-entry|1|82-postAction

    }//GEN-BEGIN:|81-entry|2|
    //</editor-fold>//GEN-END:|81-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: stopUpdateTask ">//GEN-BEGIN:|85-entry|0|86-preAction
    /**
     * Performs an action assigned to the stopUpdateTask entry-point.
     */
    public void stopUpdateTask() {//GEN-END:|85-entry|0|86-preAction
        m_CancelUpdateTask = true;
        switchDisplayable(null, getForm());//GEN-LINE:|85-entry|1|86-postAction
        m_UpdateThread = null;
    }//GEN-BEGIN:|85-entry|2|
    //</editor-fold>//GEN-END:|85-entry|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginForm ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of loginForm component.
     * @return the initialized component instance
     */
    public Form getLoginForm() {
        if (loginForm == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            loginForm = new Form(LocalizationSupport.getMessage("logger.title"), new Item[] { getStringItem4(), getSpacer1(), getScreenNameField() });//GEN-BEGIN:|89-getter|1|89-postInit
            loginForm.addCommand(getOkCommand1());
            loginForm.addCommand(getCancelCommand());
            loginForm.setCommandListener(this);//GEN-END:|89-getter|1|89-postInit
            // write post-init user code here
        }//GEN-BEGIN:|89-getter|2|
        return loginForm;
    }
    //</editor-fold>//GEN-END:|89-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenNameField ">//GEN-BEGIN:|91-getter|0|91-preInit
    /**
     * Returns an initiliazed instance of screenNameField component.
     * @return the initialized component instance
     */
    public TextField getScreenNameField() {
        if (screenNameField == null) {//GEN-END:|91-getter|0|91-preInit
            // write pre-init user code here
            screenNameField = new TextField(LocalizationSupport.getMessage("logger.nickname"), "", 32, TextField.ANY);//GEN-LINE:|91-getter|1|91-postInit
            // write post-init user code here
        }//GEN-BEGIN:|91-getter|2|
        return screenNameField;
    }
    //</editor-fold>//GEN-END:|91-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem4 ">//GEN-BEGIN:|92-getter|0|92-preInit
    /**
     * Returns an initiliazed instance of stringItem4 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem4() {
        if (stringItem4 == null) {//GEN-END:|92-getter|0|92-preInit
            // write pre-init user code here
            stringItem4 = new StringItem("", LocalizationSupport.getMessage("logger.nickname_title"));//GEN-LINE:|92-getter|1|92-postInit
            // write post-init user code here
        }//GEN-BEGIN:|92-getter|2|
        return stringItem4;
    }
    //</editor-fold>//GEN-END:|92-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ticker ">//GEN-BEGIN:|90-getter|0|90-preInit
    /**
     * Returns an initiliazed instance of ticker component.
     * @return the initialized component instance
     */
    public Ticker getTicker() {
        if (ticker == null) {//GEN-END:|90-getter|0|90-preInit
            // write pre-init user code here
            ticker = new Ticker("");//GEN-LINE:|90-getter|1|90-postInit
            // write post-init user code here
        }//GEN-BEGIN:|90-getter|2|
        return ticker;
    }
    //</editor-fold>//GEN-END:|90-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|93-getter|0|93-preInit
    /**
     * Returns an initiliazed instance of spacer1 component.
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|93-getter|0|93-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 10);//GEN-LINE:|93-getter|1|93-postInit
            // write post-init user code here
        }//GEN-BEGIN:|93-getter|2|
        return spacer1;
    }
    //</editor-fold>//GEN-END:|93-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", "Ok", Command.OK, 0);//GEN-LINE:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|94-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|96-getter|0|96-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|96-getter|0|96-preInit
            // write pre-init user code here
            cancelCommand = new Command(LocalizationSupport.getMessage("logger.cancel"), LocalizationSupport.getMessage("logger.cancel"), Command.EXIT, 1);//GEN-LINE:|96-getter|1|96-postInit
            // write post-init user code here
        }//GEN-BEGIN:|96-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|96-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            alert = new Alert(LocalizationSupport.getMessage("logger.title"), LocalizationSupport.getMessage("error.net"), null, null);//GEN-BEGIN:|99-getter|1|99-postInit
            alert.addCommand(getOkCommand2());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);//GEN-END:|99-getter|1|99-postInit
            // write post-init user code here
        }//GEN-BEGIN:|99-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">//GEN-BEGIN:|100-getter|0|100-preInit
    /**
     * Returns an initiliazed instance of okCommand2 component.
     * @return the initialized component instance
     */
    public Command getOkCommand2() {
        if (okCommand2 == null) {//GEN-END:|100-getter|0|100-preInit
            // write pre-init user code here
            okCommand2 = new Command("Ok", "Ok", Command.OK, 0);//GEN-LINE:|100-getter|1|100-postInit
            // write post-init user code here
        }//GEN-BEGIN:|100-getter|2|
        return okCommand2;
    }
    //</editor-fold>//GEN-END:|100-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image2 ">//GEN-BEGIN:|103-getter|0|103-preInit
    /**
     * Returns an initiliazed instance of image2 component.
     * @return the initialized component instance
     */
    public Image getImage2() {
        if (image2 == null) {//GEN-END:|103-getter|0|103-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|103-getter|1|103-@java.io.IOException
                image2 = Image.createImage("/agt_web.png");
            } catch (java.io.IOException e) {//GEN-END:|103-getter|1|103-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|103-getter|2|103-postInit
            // write post-init user code here
        }//GEN-BEGIN:|103-getter|3|
        return image2;
    }
    //</editor-fold>//GEN-END:|103-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font ">//GEN-BEGIN:|104-getter|0|104-preInit
    /**
     * Returns an initiliazed instance of font component.
     * @return the initialized component instance
     */
    public Font getFont() {
        if (font == null) {//GEN-END:|104-getter|0|104-preInit
            // write pre-init user code here
            font = Font.getDefaultFont();//GEN-LINE:|104-getter|1|104-postInit
            // write post-init user code here
        }//GEN-BEGIN:|104-getter|2|
        return font;
    }
    //</editor-fold>//GEN-END:|104-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image3 ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of image3 component.
     * @return the initialized component instance
     */
    public Image getImage3() {
        if (image3 == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|105-getter|1|105-@java.io.IOException
                image3 = Image.createImage("/web.png");
            } catch (java.io.IOException e) {//GEN-END:|105-getter|1|105-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|105-getter|2|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|3|
        return image3;
    }
    //</editor-fold>//GEN-END:|105-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|106-getter|0|106-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|106-getter|0|106-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|106-getter|1|106-@java.io.IOException
                image1 = Image.createImage("/logo.png");
            } catch (java.io.IOException e) {//GEN-END:|106-getter|1|106-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|106-getter|2|106-postInit
            // write post-init user code here
        }//GEN-BEGIN:|106-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|106-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem5 ">//GEN-BEGIN:|107-getter|0|107-preInit
    /**
     * Returns an initiliazed instance of stringItem5 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem5() {
        if (stringItem5 == null) {//GEN-END:|107-getter|0|107-preInit
            // write pre-init user code here
            stringItem5 = new StringItem("", "");//GEN-LINE:|107-getter|1|107-postInit
            // write post-init user code here
        }//GEN-BEGIN:|107-getter|2|
        return stringItem5;
    }
    //</editor-fold>//GEN-END:|107-getter|2|





    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
        setupSystemProperties();
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
        if (m_LogSessionID != null && !m_LogSessionID.equals("")){
            closeSession(m_LogSessionID);
        }
        m_LogSessionID = "";
    }

}
